public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int ret = solution.myAtoi("-2147483649");
        System.out.println(ret);
    }
}

class Solution {
    public int myAtoi(String s) {

        int ret = 0;

        int sign = 1;
        int i = 0;
        while(i < s.length() && s.charAt(i) == ' ') i++;
        if(i == s.length()){
            return 0;
        }

        if(s.charAt(i) == '-') {
            sign = -1;
            i++;
        }
        else if(s.charAt(i) == '+') {
            sign = 1;
            i++;
        }

        while(i < s.length() && Character.isDigit(s.charAt(i))){
            int digit = s.charAt(i) - '0';
            if (ret > Integer.MAX_VALUE / 10 || ret == Integer.MAX_VALUE / 10 && sign * digit > Integer.MAX_VALUE % 10){
                return Integer.MAX_VALUE;
            }
            else if (ret < Integer.MIN_VALUE / 10 || ret == Integer.MIN_VALUE / 10 && sign * digit < Integer.MIN_VALUE % 10){
                return Integer.MIN_VALUE;
            }

            ret = ret * 10 + digit * sign;
            i++;
        }
        return ret;
    }
}